package com.cn.api.controller;

import com.cn.api.error.ExceptionExpand;
import com.cn.api.resource.BaseResultVo;
import com.cn.api.util.ConfigProperties;
import com.cn.api.util.FileUploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Api(tags = { "上传操作" })
@RestController
@RequestMapping("/uploadImg")
public class UploadImgController extends BaseController {

	@ResponseBody
	@RequestMapping(value = "uploadImg", method = RequestMethod.POST, produces = Res_Type)
	@ApiOperation(value = "上传图片", httpMethod = "POST", response = BaseResultVo.class, notes = "上传图片")
	public String uploadImg(
			@ApiParam(name = "img", value = "上传图片", required = true) @RequestPart(value = "img", required = true) MultipartFile img,
			HttpServletRequest request) throws ExceptionExpand {
		String imgName = null;
		try {
			imgName = FileUploadUtil.upload(img, ConfigProperties.getImageUploadPath());
		} catch (IOException e) {
			return buildFailedResultInfo(500, "图片上传失败");
		}
		return buildSuccessResultInfo(imgName);
	}
	@ResponseBody
	@RequestMapping(value = "uploadVideo", method = RequestMethod.POST, produces = Res_Type)
	@ApiOperation(value = "上传视频", httpMethod = "POST", response = BaseResultVo.class, notes = "上传视频")
	public String uploadVideo(
			@ApiParam(name = "video", value = "上传视频", required = true) @RequestPart(value = "video", required = true) MultipartFile video,
			HttpServletRequest request) throws ExceptionExpand {
		String videoName = null;
		try {
			videoName = FileUploadUtil.uploadVideo(video, ConfigProperties.getImageUploadPath());
		} catch (IOException e) {
			return buildFailedResultInfo(500, "视频传失败");
		}
		return buildSuccessResultInfo(videoName);
	}
}
